package com.wang.auth.listener;

import com.wang.auth.service.SysLoginLogService;
import com.wang.model.system.SysLoginLog;
import com.wang.security.custom.CustomUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Objects;

/**
 * @author wjh
 * @create 2023-06-14
 */
public class AuthenticationFailureListener implements ApplicationListener<AbstractAuthenticationFailureEvent> {
    @Autowired
    private SysLoginLogService loginLogService;
    
    @Override
    public void onApplicationEvent(AbstractAuthenticationFailureEvent event) {
        String message;
        if (event instanceof AuthenticationFailureBadCredentialsEvent) {
            //提供的凭据是错误的，用户名或者密码错误
            message = "提供的凭据是错误的，用户名或者密码错误";
        } else if (event instanceof AuthenticationFailureCredentialsExpiredEvent) {
            //验证通过，但是密码过期
            message = "验证通过，但是密码过期";
        } else if (event instanceof AuthenticationFailureDisabledEvent) {
            //验证过了但是账户被禁用
            message = "验证过了但是账户被禁用";
        } else if (event instanceof AuthenticationFailureExpiredEvent) {
            //验证通过了，但是账号已经过期
            message = "验证通过了，但是账号已经过期";
        } else if (event instanceof AuthenticationFailureLockedEvent) {
            //账户被锁定
            message = "账户被锁定";
        } else if (event instanceof AuthenticationFailureProviderNotFoundEvent) {
            //配置错误，没有合适的AuthenticationProvider来处理登录验证
            message = "配置错误";
        } else if (event instanceof AuthenticationFailureProxyUntrustedEvent) {
            // 代理不受信任，用于Oauth、CAS这类三方验证的情形，多属于配置错误
            message = "代理不受信任";
        } else if (event instanceof AuthenticationFailureServiceExceptionEvent) {
            // 其他任何在AuthenticationManager中内部发生的异常都会被封装成此类
            message = "内部发生的异常";
        } else {
            message = "其他未知错误";
        }
        // 登录账号
        CustomUser user = (CustomUser) event.getAuthentication().getPrincipal();
        // 登录密码
        Object credentials = event.getAuthentication().getCredentials();
        SysLoginLog loginLog = new SysLoginLog();
//        loginLog.setIpaddr( ServletUtil.getClientIP(Objects.requireNonNull(( (ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()), ""));
        loginLog.setIpaddr("127.0.0.1");
        loginLog.setUsername( user.getSysUser().getUsername());
        loginLog.setStatus(false);
        loginLog.setMsg(message);
        loginLog.setAccessTime(LocalDateTime.now().atOffset(ZoneOffset.UTC).toLocalDateTime());
        loginLogService.save(loginLog);
    }
}
